<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>最终调试</title>
    <link rel="stylesheet" href="assets/css/style.css">
    <link rel="stylesheet" href="assets/css/ilovefree-migration.css">
    <link rel="stylesheet" href="assets/css/enhanced-layout.css">
    <link rel="stylesheet" href="assets/css/fontawesome-icons.css">
    <style>
        .debug-container { padding: 20px; }
        .debug-panel {
            background: rgba(3, 150, 255, 0.1);
            border: 1px solid var(--ilovefree-blue);
            padding: 15px;
            border-radius: 8px;
            margin: 20px 0;
        }
        .log-output {
            background: #2d3748;
            color: #e2e8f0;
            padding: 15px;
            border-radius: 8px;
            font-family: monospace;
            font-size: 14px;
            max-height: 200px;
            overflow-y: auto;
            margin: 10px 0;
        }
        .test-button {
            padding: 8px 16px;
            background: var(--ilovefree-blue);
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin: 5px;
        }
    </style>
</head>
<body>
    <div class="debug-container">
        <h1>最终调试页面</h1>

        <?php
        require_once 'config/config.php';
        require_once 'includes/database.php';
        $db = new Database();

        $categories = $db->getCategories();
        $category = null;
        foreach ($categories as $cat) {
            if ($cat['id'] == 2) {
                $category = $cat;
                break;
            }
        }

        $navigations = $db->getNavigations(2);
        $tags = $db->getTags(2);

        // 获取推荐标签信息
        $recommendTagId = 0;
        foreach ($tags as $tag) {
            if ($tag['name_cn'] === '推荐') {
                $recommendTagId = $tag['id'];
                break;
            }
        }
        ?>

        <div class="debug-panel">
            <h3>基础信息</h3>
            <p>分类: <?php echo htmlspecialchars($category['name']); ?> (ID: <?php echo $category['id']; ?>)</p>
            <p>导航总数: <?php echo count($navigations); ?></p>
            <p>推荐标签ID: <?php echo $recommendTagId; ?></p>
            <p>标签总数: <?php echo count($tags); ?></p>
        

        <!-- 标签容器 -->
        <div class="category-header">
            <div class="category-title">
                <div class="category-icon">
                <h2 class="category-name"><?php echo htmlspecialchars($category['name']); ?></h2>
            
            <div class="tags-container" id="category-2">
                <!-- 推荐标签 -->
                <button class="tag-btn active" data-tag-id="<?php echo $recommendTagId; ?>" data-tag-filter="推荐" onclick="debugFilter(<?php echo $recommendTagId; ?>, '推荐')">
                    推荐
                    <span class="tag-count">15</span>
                </button>

                <?php
                $filteredTags = array_filter($tags, function($tag) {
                    return trim($tag['name_cn']) !== '推荐';
                });
                ?>
                <?php foreach ($filteredTags as $tag): ?>
                    <?php
                    $tagNavCount = 0;
                    foreach ($navigations as $nav) {
                        if (!empty($nav['tag_ids']) && is_array($nav['tag_ids'])) {
                            if (in_array($tag['id'], $nav['tag_ids'])) {
                                $tagNavCount++;
                            }
                        }
                    }
                    ?>
                    <button class="tag-btn" data-tag-id="<?php echo $tag['id']; ?>" data-tag-filter="<?php echo htmlspecialchars($tag['name_cn']); ?>" onclick="debugFilter(<?php echo $tag['id']; ?>, '<?php echo htmlspecialchars($tag['name_cn']); ?>')">
                        <?php echo htmlspecialchars($tag['name_cn']); ?>
                        <span class="tag-count"><?php echo $tagNavCount; ?></span>
                    </button>
                <?php endforeach; ?>
            
        

        <div class="debug-panel">
            <h3>测试按钮</h3>
            <button class="test-button" onclick="showAllCards()">显示所有卡片</button>
            <button class="test-button" onclick="debugSystem()">调试系统</button>
            <button class="test-button" onclick="debugCards()">调试卡片</button>
        

        <!-- 导航卡片 -->
        <div class="nav-grid" id="nav-grid-2">
            <?php foreach ($navigations as $nav): ?>
                <?php
                $tagFilters = [];
                if (!empty($nav['tag_ids']) && is_array($nav['tag_ids'])) {
                    foreach ($nav['tag_ids'] as $tagId) {
                        foreach ($tags as $tag) {
                            if ($tag['id'] == $tagId) {
                                $tagFilters[] = htmlspecialchars($tag['name_cn']);
                                break;
                            }
                        }
                    }
                }
                $tagFiltersString = implode(',', array_unique($tagFilters));
                ?>
                <div class="nav-card" data-tag-filters="<?php echo $tagFiltersString; ?>">
                    <a href="<?php echo htmlspecialchars($nav['url']); ?>" target="_blank">
                        
                            <h4><?php echo htmlspecialchars($nav['name']); ?></h4>
                            <p><?php echo htmlspecialchars($nav['description']); ?></p>
                        
                    </a>
                
            <?php endforeach; ?>
        

        <div class="debug-panel">
            <h3>调试日志</h3>
            <div class="log-output" id="debug-log">
        
    

    <script>
        let debugLogs = [];

        function log(message) {
            console.log(message);
            debugLogs.push(`[${new Date().toLocaleTimeString()}] ${message}`);
            updateLogDisplay();
        }

        function updateLogDisplay() {
            const logDiv = document.getElementById('debug-log');
            if (logDiv) {
                logDiv.innerHTML = debugLogs.slice(-30).join('<br>');
                logDiv.scrollTop = logDiv.scrollHeight;
            }
        }

        function debugFilter(tagId, tagName) {
            log(`🏷️ 调试过滤: tagId=${tagId}, tagName="${tagName}"`);

            // 更新按钮状态
            document.querySelectorAll('.tag-btn').forEach(btn => {
                btn.classList.remove('active');
            });
            const activeBtn = document.querySelector(`[data-tag-id="${tagId}"]`);
            if (activeBtn) {
                activeBtn.classList.add('active');
                log(`✅ 激活按钮: ${tagName}`);
            }

            // 过滤卡片
            const cards = document.querySelectorAll('.nav-card');
            let visibleCount = 0;

            cards.forEach(card => {
                const filters = card.getAttribute('data-tag-filters') || '';
                const tags = filters.split(',').map(tag => tag.trim()).filter(tag => tag);

                log(`🔍 检查卡片: ${card.querySelector('h4').textContent}`);
                log(`   - 标签: [${tags.join(', ')}]`);
                log(`   - 包含'${tagName}': ${tags.includes(tagName)}`);

                if (tags.includes(tagName)) {
                    card.style.display = 'block';
                    visibleCount++;
                    log(`   - ✅ 显示卡片`);
                } else {
                    card.style.display = 'none';
                    log(`   - ❌ 隐藏卡片`);
                }
            });

            log(`🎯 过滤完成，显示 ${visibleCount} 个卡片`);
        }

        function showAllCards() {
            document.querySelectorAll('.nav-card').forEach(card => {
                card.style.display = 'block';
            });
            log('✅ 显示所有卡片');
        }

        function debugSystem() {
            log('=== 系统调试信息 ===');
            log(`卡片总数: ${document.querySelectorAll('.nav-card').length}`);
            log(`标签按钮总数: ${document.querySelectorAll('.tag-btn').length}`);
            log(`迁移系统状态: ${window.phpPickfreeMigration ? '已初始化' : '未初始化'}`);

            if (window.phpPickfreeMigration) {
                const category2 = window.phpPickfreeMigration.categories['category-2'];
                if (category2) {
                    log(`分类2卡片数: ${category2.cards.length}`);
                    log(`分类2标签统计: ${JSON.stringify(category2.tagCounts)}`);
                } else {
                    log('❌ 分类2数据未找到');
                }
            }
        }

        function debugCards() {
            log('=== 卡片调试信息 ===');
            document.querySelectorAll('.nav-card').forEach((card, index) => {
                const title = card.querySelector('h4').textContent;
                const filters = card.getAttribute('data-tag-filters');
                log(`卡片${index + 1}: ${title} -> [${filters}]`);
            });
        }

        document.addEventListener('DOMContentLoaded', () => {
            log('🎯 页面加载完成');
            log(`卡片总数: ${document.querySelectorAll('.nav-card').length}`);

            setTimeout(() => {
                debugSystem();
                debugCards();
                log('🧪 自动测试推荐标签...');
                debugFilter(<?php echo $recommendTagId; ?>, '推荐');
            }, 1000);
        });
    </script>
</body>
</html>